/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on 15.08.2007
 * @author Asia Grzibovska
 *
 */

package uk.ac.sanger.servlet;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

import uk.ac.sanger.*;
/**
 * Servlet implementation class for Servlet: writebackHandleCoordinates
 * the servlet compares coordinates.xml with database content 
 * and adds missing coordinates tables 
 *
 */
public class WritebackHandleCoordinates extends HttpServlet implements Servlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static final String WRITEBACK_SERVER = "http://localhost:8080/writebackServlet/writebackServlet";
	public static long ONCEPEHOUR = 1000 * 60 * 60 ;
	
	Timer timer ;
	
	public void init(ServletConfig config)
	throws ServletException
	{
		super.init(config);

		System.setProperty("proxySet", "true");
        System.setProperty("proxyHost", "wwwcache.sanger.ac.uk");
        System.setProperty("proxyPort", "3128");

		
		TimerTask fetchCoords  = new CoordinateTimer();
		timer =  new Timer();

		timer.schedule(fetchCoords,ONCEPEHOUR, ONCEPEHOUR);
	}
	
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
		System.out.println("canceling coordinates timers");
		timer.cancel();
	}

	//	This is the all-important doPost() method.
	public void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String mode="";
		String system="";
		String system_id ="";
		String system_name="";
		String system_uri="";

		try {		

			//exchange data with JSP through session attributes
			HttpSession session = request.getSession(true);
			if (session != null) {
				//coordinate_update();
				
				//session.setAttribute("coordinates", list_coordinates);

				// 1=add,2=modify,3=delete
				mode = request.getParameter("group1");	
				system = request.getParameter("system");

				//get value of dropdown list : 101;xxx_coordinate
				int separator=system.indexOf(";");
				int separator2=system.indexOf(";",separator+1);
				system_id = system.substring(0, separator);
				system_name = system.substring(separator+1, separator2);
				system_uri = system.substring(separator2+1, system.length()-1);

				//mode 1=add,2=modify,3=delete
				session.setAttribute("mode", mode);
				session.setAttribute("system", system_id);
				session.setAttribute("system_name", system_name);
				session.setAttribute("system_uri", system_uri);
				
			}

		   if((String)session.getAttribute("openid")==null) {
				String message = "you are not logged in, please log in first!";
				session.setAttribute("message", message);

				response.sendRedirect("login.jsp");
				return;
			}
	
			if (mode.equals("3")||mode.equals("2"))
				//send redirect to show features
				response.sendRedirect("show_features.jsp?arg=3&action=show&group=&uri="+system_uri);	
			else
				//send redirect to feature upload page
				response.sendRedirect("writeback.jsp?uri="+system_uri+"&start=10&end=20");
		}
		catch (Exception err){
			err.printStackTrace(); }
	}

	// doGet method, not really used
	public void doGet(HttpServletRequest request,
			HttpServletResponse response)
	throws IOException, ServletException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter( );
		out.println ("<html><body>WritebackGroups</body></html>");
		out.println("<HTML><HEAD><TITLE>Servlet</TITLE></HEAD></HTML>");
	}
    
	//update coordinates list
	public void coordinate_update(){
		try
		{
			//compare XML with data in db
			UpdateCoordinate uc =new UpdateCoordinate();
			uc.makeUpdateCoordinates();					
		}

		catch (Exception err){
			err.printStackTrace(); }
	}
}

